import re
import requests
from bs4 import BeautifulSoup as BS 

def GetPage(url):
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
        'Host': 'kaijiang.zhcw.com',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
    }
    response = requests.get(url,headers=headers)
    text=BS(response.text,'lxml')
    return text


def GetText():
    for i in range(1,120):
        url='http://kaijiang.zhcw.com/zhcw/html/ssq/list_{}.html'.format(i)
        print(url)
        text=GetPage(url)
        ems=text.find_all('em')
        divs=text.find_all('td',{'align':'center'})

        n=0
        with open('./result.txt','a')as f:
            for em in ems:
                message=em.get_text()
                n=n+1
                if n==7:
                    n=0
                    message=message+"\n"
                else:
                    message+='\t'
                f.write(str(message))


#看哪个数字出现次数最多
def shownum():
    redball=[]
    blueball=[]
    with open('./result.txt','r')as f:
        for i in range(2400):
            data=f.readline().split('\t')
            redball.extend([int(i) for i in data[:-1]])
            blueball.extend([int(data[-1])])

    redcount=Counter(redball).most_common()
    bluecount=Counter(blueball).most_common()

    red_dict={}
    blue_dict={}
    for i in redcount:
        print(i)
        red_dict['{}'.format(i[0])]=i[1]
    for j in bluecount:
        print(j)
        blue_dict['{}'.format(j[0])]=j[1]







if __name__=='__main__':
   # GetText()
   shownum